package com.xb.loan.util.qrtz.task.impl;


import com.xb.loan.util.StringUtils;
import com.xb.loan.util.qrtz.common.bean.TaskEntity;
import com.xb.loan.util.qrtz.common.store.TaskEntiryStoreSupport;
import com.xb.loan.util.qrtz.task.AbstractCronTask;
import com.xb.loan.util.qrtz.task.AbstractTask;
import com.xb.loan.util.qrtz.task.Task;

import java.util.List;

/**
 * 
 * 测过期没有触发调度的任务，重新调度此任务
 * @author hyd
 */
public class CheckMisFireTask extends AbstractCronTask {

    public CheckMisFireTask() {
        super();
    }

    public CheckMisFireTask(String jobName) {
        super(jobName);
    }

    @Override
    public void run() {

        TaskEntiryStoreSupport storeSupport = getTaskStoreSupport();
        if (storeSupport == null) return;

        List<TaskEntity> list = storeSupport.getMisFireTaskEntity();

        if (list == null || list.size() == 0) return;
        logger.info("  MisFireTask  size=" + list.size());
        for (TaskEntity _taskEntity : list) {
            try {
                Object obj = getClass().forName(_taskEntity.getClassName()).newInstance();

                if (obj instanceof AbstractTask) {
                    AbstractTask temp = (AbstractTask) obj;
                    temp.setJobName(StringUtils.paser(_taskEntity.getId(), "$")[0]);
                    temp.setParams(_taskEntity.getTask_date());
                }

                if (obj instanceof AbstractCronTask) {
                    AbstractCronTask temp = (AbstractCronTask) obj;
                    temp.setCron_expression(_taskEntity.getCron());
                }
                getTaskScheduleManager().reShedule((Task) obj);

            } catch (Exception e) {
                logger.error("", e);
            }
        }

    }

}
